<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.Map.*"%>
<%@ page import="com.gzgi.framework.*"%>
<%@ page import="com.gzgi.framework.util.*"%>
<%@ page import="com.gzgi.framework.context.*"%>
<%@ page import="com.gzgi.framework.entity.*"%>
<%@ include file="/pages/system/checkAdmin.jsp"%>
<link rel="stylesheet" type="text/css"
	href="<%=request.getContextPath()%>/pages/styles/styles.css" />
<%
 
    String sql = request.getParameter("sql");
	if(sql != null){
		SQLFormatter f = new SQLFormatter();
		sql = f.format(sql);
		 
		List rows = null;
		StringBuffer buffer = new StringBuffer();
		StringBuffer header = new StringBuffer();
		StringBuffer sb = new StringBuffer();
		buffer.append("<table align=\"center\" class=\"x-table-border\" cellspacing=\"1\"")
			  .append(" cellpadding=\"4\" width=\"98%\" nowrap>");
 			 
			if(sql.trim().toLowerCase().startsWith("select")){
				System.out.println(sql);
				 PersistenceService svc = (PersistenceService)ContextFactory.getBean("persistenceService"); 
                 rows = svc.getList(sql);
				 if(rows != null && rows.size() > 0){
					     header.append("<tr class=\"x-title\">\n");
						 int index = 0;
						 Collection names = new HashSet();
                         for(Object object : rows){
							    sb.append("<tr class=\"x-content\">\n");
							    Map dataMap = Tools.getDataMap(object);
							    Set<Entry<String, Object>> entrySet = dataMap.entrySet();
								for (Entry<String, Object> entry : entrySet) {
									String key = entry.getKey();
									Object value = entry.getValue();
									 if(index == 0){
                                         names.add(key);
                                         header.append("<td>");
										 header.append(key);
										 header.append("</td>");
										 sb.append("<td nowrap>");
										 if(value != null){
										   sb.append(value);
										 }else{
											 sb.append("&nbsp;");
										 }
										 sb.append("</td>");
									  } else {
                                        if(names.contains(key) ){
                                         sb.append("<td nowrap>");
										  if(value != null){
										   sb.append(value);
										  }else{
											 sb.append("&nbsp;");
										 }
										 sb.append("</td>");
									    }
								    }//else
								}//for
							sb.append("\n</tr>");
                            index++;
						  }//for
                         header.append("\n</tr>");
						 buffer.append(header.toString());
						 buffer.append(sb.toString());
				 }
			}
			buffer.append("</table>");
			out.println("<br><div align=center>Commond OK</div><br>");
			out.println(buffer.toString());
	}
%>
<br>
<center>
<form method="post" action=""><textarea name="sql" rows="20"
	cols="86">
<%=sql != null ? sql : ""%>
</textarea> <br>
<br>
<input type="submit" name="submit" value="submit"></form>
</center>